# -*- coding: utf-8 -*-
import scrapy
import json
import re
import requests

class DySpider(scrapy.Spider):
    name = 'dy'
    allowed_domains = ['douyu.com']
    start_urls = ['https://www.douyu.com/gapi/rknc/directory/yzRec/1']


    def parse(self, response):
        print(type(response.text), '------------------------')
        imgs_url = re.findall('"rs16":"(.*?)"', response.body.decode('utf-8'))
        print(imgs_url)
        for img_url in imgs_url:
            item = {}
            item['img_name'] = img_url[-3:]
            item['img_b'] = requests.get(img_url).content
            yield item


        page_num = response.request.url.split('/')[-1]
        next_url = re.sub(page_num, str(int(page_num) + 1), response.request.url)
        if int(page_num) <= 6:
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )
